package com.letv.android.young.client.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import com.ledim.utils.ConstantUtil;
import com.sina.weibo.sdk.api.share.BaseRequest;
import com.sina.weibo.sdk.api.share.BaseResponse;
import com.sina.weibo.sdk.api.share.IWeiboHandler;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.constant.WBConstants;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.ledim.utils.LogUtil;
import com.ledim.utils.ToastUtil;
import org.greenrobot.eventbus.EventBus;

public class WXEntryActivity extends Activity
        implements IWXAPIEventHandler, IWeiboHandler.Request, IWeiboHandler.Response {

    private IWXAPI mWeixinAPI;
    private IWeiboShareAPI mWeiboShareAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 注册微信
        if (null == mWeixinAPI) {
            mWeixinAPI =
                    WXAPIFactory.createWXAPI(this, ConstantUtil.ShareConstant.WEIXIN_APP_ID, true);
            mWeixinAPI.registerApp(ConstantUtil.ShareConstant.WEIXIN_APP_ID);
        }
        mWeixinAPI.handleIntent(getIntent(), this);
        //
        //// 注册QQ
        //mTencent =
        //        Tencent.createInstance(Config.QQZone_API_ID, WXEntryActivity.this.getApplicationContext());
        //
        // 注册微博
        mWeiboShareAPI =
                WeiboShareSDK.createWeiboAPI(this, ConstantUtil.ShareConstant.SINA_APP_KEY);
        if (savedInstanceState != null) {
            mWeiboShareAPI.handleWeiboRequest(getIntent(), this);
        }
        // 处理微博客户端发送过来的请求
        if (null != mWeiboShareAPI) {
            mWeiboShareAPI.handleWeiboRequest(getIntent(), this);
            mWeiboShareAPI.handleWeiboResponse(getIntent(), this);
        }
        finish();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        // 处理微博客户端发送过来的请求
        if (null != mWeiboShareAPI) {
            mWeiboShareAPI.handleWeiboRequest(intent, this);
            mWeiboShareAPI.handleWeiboResponse(intent, this);
        }
        mWeixinAPI.handleIntent(intent, this);
        LogUtil.e("wxentry  onNewIntent");
    }

    @Override
    public void onReq(BaseReq baseReq) {
        LogUtil.e("wxentry  onReq");
        finish();
    }

    /**
     * 微信分享回调函数
     *
     * @param baseResp
     */
    @Override
    public void onResp(BaseResp baseResp) {
        LogUtil.e("wxentry weixin onResp");
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                try {
                    // 微信登录成功
                    SendAuth.Resp resp_auth = (SendAuth.Resp) baseResp;
                    // 发消息得到code
                    Message msg = new Message();
                    msg.what = ConstantUtil.EventBusMsgConstant.WEIXIN_LOGIN;
                    msg.obj = resp_auth.code;
                    EventBus.getDefault().post(msg);
                } catch (ClassCastException e) {
                    ToastUtil.toast("分享成功");
                }
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                ToastUtil.toast("拒绝授权");

                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                try {
                    ToastUtil.toast("取消授权");
                } catch (ClassCastException e) {
                    ToastUtil.toast("取消分享");
                }
                break;
            case BaseResp.ErrCode.ERR_SENT_FAILED:
                ToastUtil.toast("请重试");
                break;

        }
        finish();
    }

    @Override
    public void onRequest(BaseRequest baseRequest) {
        LogUtil.e("wxentry  onRequest");
        finish();
    }

    @Override
    public void onResponse(BaseResponse baseResponse) {
        LogUtil.e("wxentry  onResponse");
        switch (baseResponse.errCode) {
            case WBConstants.ErrorCode.ERR_OK:
                ToastUtil.toast("分享成功");
                break;
            case WBConstants.ErrorCode.ERR_CANCEL:
                ToastUtil.toast("取消分享");
                break;
            case WBConstants.ErrorCode.ERR_FAIL:
                ToastUtil.toast("请重试" + baseResponse.errMsg);
                break;
        }
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtil.e("wxentry  onpause");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtil.e("wxentry onActivityResult");
    }
}